package com.itheima.controller;



import org.springframework.web.bind.annotation.RequestMapping;




import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/role")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private IdWorker idWorker;
    /**
     * 增加角色
     * @param role
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Role role){
        role.setUid(DigestUtils.md5DigestAsHex((idWorker.nextId()+"").getBytes()));
        roleService.add(role);
        return new Result("success","增加成功");
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Role role){
        roleService.edit(role);
        return new Result("success","修改成功");
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Role role){
        roleService.delete(role);
        return new Result("success","删除成功");
    }

    /**
     * 分页查询角色
     * @param searchMap
     * @return
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String,Object> searchMap){
        Page<Role>rolePage=roleService.findPage(searchMap);
        PageResult<Role> rolePageResult = new PageResult<>(rolePage.getTotal(), rolePage.getResult());
        return new Result("success", rolePageResult);
    }
}

